
/****************************************************************************/
/*Copyright (c) 2011, Florent DEVILLE.                                      */
/*All rights reserved.                                                      */
/*                                                                          */
/*Redistribution and use in source and binary forms, with or without        */
/*modification, are permitted provided that the following conditions        */
/*are met:                                                                  */
/*                                                                          */
/* - Redistributions of source code must retain the above copyright         */
/*notice, this list of conditions and the following disclaimer.             */
/* - Redistributions in binary form must reproduce the above                */
/*copyright notice, this list of conditions and the following               */
/*disclaimer in the documentation and/or other materials provided           */
/*with the distribution.                                                    */
/* - The names of its contributors cannot be used to endorse or promote     */
/*products derived from this software without specific prior written        */
/*permission.                                                               */
/* - The source code cannot be used for commercial purposes without         */ 
/*its contributors' permission.                                             */
/*                                                                          */
/*THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS       */
/*"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT         */
/*LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS         */
/*FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE            */
/*COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,       */
/*INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,      */
/*BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;          */
/*LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER          */
/*CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT        */
/*LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN         */
/*ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE           */
/*POSSIBILITY OF SUCH DAMAGE.                                               */
/****************************************************************************/

#include "RTTypes.h"
#include "RTPrimSphere.h"

#include <math.h>
#include "RTPolynomial.h"

namespace RT
{
	/*Constructor*/
	RTPrimSphere::RTPrimSphere(F32 radius):RTIPrimitive(), m_radius(radius)
	{}

	/*Destructor*/
	RTPrimSphere::~RTPrimSphere(){}

	/*Calculate the intersection between a ray and the sphere*/
	bool RTPrimSphere::intersect(const RTRay& ray, F32& dist, bool useGlobalCoordinates)
	{
		//change the coordinate system
		RTRay r;
		if (useGlobalCoordinates)
			globalToLocal(ray, r);
		else
			convertRayToLocalCoordinates(ray, r);

		//RTPolynomial coefficient
		RT::F32 A = r.getDirection().norme2();
		RT::F32 B = 2 * r.getDirection().dot(r.getOrigin());
		RT::F32 C = r.getOrigin().norme2() - m_radius * m_radius;

		//get solutions
		RTPolynomial poly(A, B, C);
		F64 solutions[2] = {0, 0};
		poly.SolveQuadratic(solutions);

		//no solutions
		if(solutions[0] <= 0 && solutions[1] <= 0)
			return false;

		//get the minimal solution
		else if(solutions[0] <= 0)
			dist = (F32)solutions[1];
		else if (solutions[1] <= 0)
			dist = (F32)solutions[0];
		else
		{
			if(solutions[0] < solutions[1])
				dist = (F32)solutions[0];
			else
				dist = (F32)solutions[1];
		}
		return true;
	}

	/*
	To calculate the normal of a sphere, we use the gradient of the sphere's equation
	Equation : x^2 + y^2 + z^2 - r^2 = 0
	Gradiant = 2x + 2y + 2z
	We can simplify it by two to have N(x, y, z)
	*/
	void RTPrimSphere::computeNormal(const RTRay& ray, F32 dist, RTVector3f& normal)const
	{
		//change the coordinate system
		RTRay r;
		convertRayToLocalCoordinates(ray, r);

		//calculate the normal
		normal = r.getOrigin() + r.getDirection()*dist;
		normal.normalize();

		//if we are inside, reverse the normal
		if(r.getDirection().dot(normal)>0)
			normal = normal * -1;

		//reset to the global coordinate systeme
		convertNormalToGlobalCoordinates(normal, normal);

	}

	/*Check if the point is inside the sphere*/
	bool RTPrimSphere::isInside(const RTVector3f& p)const
	{
		//convert p to local coordinates
		RTVector3f localP;
		convertPointToLocalCoordinates(p, localP);

		if(localP.norme2() <= m_radius * m_radius)
			return true;

		return false;
	}

}
